<?php
namespace app\admin\controller;
use \app\admin\controller\BaseController;
use app\admin\model\Categroy;
use app\admin\model\Carousel as Carouseldb;
use app\admin\model\Shop;
class Carousel extends BaseController
{
    /**
     * 首页轮播图展示
     * @param Carouseldb $carousel
     * @return \think\response\View
     */

    public function index(Carouseldb $carousel)
    {
        $this->isLogin();
        $result = $carousel->select()->toArray();
        return view('carousel/carousel',compact('result'));
    }

    /**
     * 首页轮播图修改
     * @param Carouseldb $carousel
     */
    public function update(Carouseldb $carousel)
    {
        $this->isLogin();
        $firstName = input('firstName');
        $secondName = input('secondName');
        $thirdName = input('thirdName');
        $count = $carousel->where('categroy_id', input('categroy'))->where('shop_id', input('shop_id'))->count();
        if ($count == '0') {
            $data['shop_id'] = input('shop_id');
            $data['categroy_id'] = input('categroy');
            $data['classify'] = $firstName . '>' . $secondName . '>' . $thirdName;
            $data['update_time'] = time();
            $files = request()->file('img');
            $info = $files->move('upload');
            if (!$info) return $this->error('轮播图不能为空');
            $data['image_addr'] = '\\upload\\' . date('Ymd') . '\\' . $info->getFilename();
            $result = $carousel->where(['id' => input('id')])->update($data);
            if (!$result)  $this->error('修改失败');
            return $this->redirect($_SERVER["HTTP_REFERER"]);
        } elseif ($count != '0') {
            $files = request()->file('img');
            $info = $files->move('upload');
            if (!$info) return $this->error('轮播图不能为空');
            $data['image_addr'] = '\\upload\\' . date('Ymd') . '\\' . $info->getFilename();
            $data['update_time'] = time();
            $result = $carousel->where(['id' => input('id'),'categroy_id'=>input('categroy'),'shop_id'=>input('shop_id')])->update($data);
            if (!$result) $this->error('该分类该商品图片已存在,操作失败');
            return $this->redirect($_SERVER["HTTP_REFERER"]);
        }
    }

//        if($cateCount == '0' || ($cateCount !='0' && $shopCount == '0')){
//            $files = request()->file('img');
//            $info = $files->move('upload');
//            if (!$info) return $this->error('轮播图不能为空');
//            $data['image_addr'] = '\\upload\\' . date('Ymd').'\\'.$info->getFilename();
//            $data['update_time'] = time();
//            $result = $carousel->where(['id' => input('id')])->update($data);
//            if(!$result)  return $this->error('修改失败');
//            return $this->redirect($_SERVER["HTTP_REFERER"]);
//        }elseif($cateCount != '0' && $shopCount != '0'){
//            $files = request()->file('img');
//            $info = $files->move('upload');
//            if (!$info) return $this->error('轮播图不能为空');
//            $data['image_addr'] = '\\upload\\' . date('Ymd').'\\'.$info->getFilename();
//            $data['update_time'] = time();
//            $result = $carousel->where(['id' => input('id')])->update($data);
//            if(!$result)  return $this->error('修改失败');
//            return $this->redirect($_SERVER["HTTP_REFERER"]);
//        }
//        return $this->error('该分类该商品图片已存在,操作失败');
//    }
    /**
     * 获取分类列表
     */
    public function getCategroy(Categroy $categroy)
    {
        $this->isLogin();
        $type = input('post.type');
        $categroy_id = input('post.categroy');

        if(!$type) {
            $result = $categroy->where(['pid' => 0, 'is_use' => 0, 'grade' => 1])->select()->toArray();
        } else if ($type == '1') {
            $result = $categroy->where(['pid' => $categroy_id, 'is_use' => 0, 'grade' => 2])->select()->toArray();
        } else {
            $result = $categroy->where(['pid' => $categroy_id, 'is_use' => 0, 'grade' => 3])->select()->toArray();
        }
        return json_encode($result,  JSON_UNESCAPED_UNICODE);
    }
    /**
     * 获取分类下的所有商品
     */
    public function getCategroyGoods(Shop $shop)
    {
        $this->isLogin();
        $goods = $shop->where('categroy_id',input('categroy'))->field('id,shop_name,categroy_id')->select()->toArray();
        return json($goods);
    }
}